package base

import "sort"

/*
* @author: hjz
* @time: 2025/4/26 22:27
* 先排序，然后  【a,b】[c,d]    只有c>b 的时候 【a,b】结束， c<b&& d>b [a,d]
 */
func merge(intervals [][]int) [][]int {
	sort.Slice(intervals, func(i, j int) bool {
		return intervals[i][0] < intervals[j][0]
	})
	res := make([][]int, 0, len(intervals)>>1)
	l, r := intervals[0][0], intervals[0][1] // 左 ,右
	for i := 1; i < len(intervals); i++ {
		nl, nr := intervals[i][0], intervals[i][1]
		if nl > r {
			res = append(res, []int{l, r})
			l, r = nl, nr
		}
		if nr > r {
			r = nr
		}
	}
	res = append(res, []int{l, r})
	return res
}
